package com.app.micaihu.view.home.questions.fragment

import android.os.Bundle
import androidx.lifecycle.Observer
import com.app.base.BaseMvpFragment
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.base.BaseOldActivity
import com.app.micaihu.configure.Config
import com.app.micaihu.configure.LoadType
import com.app.micaihu.configure.UmEventId
import com.app.micaihu.manager.UserInforManager
import com.app.micaihu.utils.MobclickAgentUtils
import com.app.micaihu.utils.StatServiceUtils
import com.app.micaihu.view.bean.QuestionList
import com.app.micaihu.view.main.MainActivity
import com.app.micaihu.view.home.questions.activity.QuestionDetailActivity
import com.app.micaihu.view.home.questions.adapter.QuestionsListAdapter
import com.app.micaihu.view.home.questions.contract.QuestionsContract
import com.app.micaihu.view.home.questions.presenter.QuestionsPresenter
import com.app.micaihu.view.main.topic.PostSendActivity
import com.app.micaihu.view.main.view.WrapContentLinearLayoutManager
import com.app.utils.biz.Biz
import com.blankj.utilcode.util.*
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.question_fragment_list.*

/**
 * 问答
 */
class QuestionsListFragment : BaseMvpFragment<QuestionsContract.View, QuestionsPresenter>(), QuestionsContract.View {

    override fun getPresenter() = QuestionsPresenter()

    override fun getLayoutId() = R.layout.question_fragment_list

    //操作监听
    private var mOnRefreshingListener: MainActivity.OnRefreshingListener? = null
    private val questionsListAdapter: QuestionsListAdapter by lazy {
        val questionsListAdapter = QuestionsListAdapter()
        rvList.layoutManager = WrapContentLinearLayoutManager(context)
        rvList.adapter = questionsListAdapter

        questionsListAdapter.loadMoreModule.isAutoLoadMore = true
        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        questionsListAdapter.loadMoreModule.isEnableLoadMoreIfNotFullPage = false
        questionsListAdapter
    }

    override fun setData() {
        LiveEventBus.get(Config.EventBus.questionSuccess, Boolean::class.java).observe(this, Observer {
            ViewUtils.runOnUiThreadDelayed({
                mPresenter?.refresh()
            }, 200)
        })
        LiveEventBus.get(Config.EventBus.questionDel, Boolean::class.java).observe(this, Observer {
            ViewUtils.runOnUiThreadDelayed({
                mPresenter?.refresh()
            }, 200)
        })
    }

    override fun lazyLoad() {
        questionsListAdapter.setEmptyView(loadingView)
        mPresenter?.refresh()
    }

    override fun addListeners() {
        refreshLayout.setOnRefreshListener {
            // 这里的作用是防止下拉刷新的时候还可以上拉加载
            questionsListAdapter.loadMoreModule.isEnableLoadMore = false
            mPresenter?.refresh()
        }
        questionsListAdapter.loadMoreModule.setOnLoadMoreListener {
            mPresenter?.loadMore()
        }
        questionsListAdapter.setOnItemClickListener { adapter, view, position ->
            questionsListAdapter.getItem(position).let {
                StatServiceUtils.event(Config.EventBauduId.answerlist_click, "问答列表点击")
                MobclickAgentUtils.event(UmEventId.answer_click, "问答列表_点击")

                val bundle = Bundle()
                bundle.putString(Config.Extra.PARAMETER1, it.id)
                enterPager(QuestionDetailActivity::class.java, bundle)
            }
        }
        ivQuestion.setOnClickListener {
            if (StringUtils.isEmpty(Biz.instance.uid())) {
                UserInforManager.getInstance().toLoginPage(activity)
                return@setOnClickListener
            }
            val bundle = Bundle()
            bundle.putBoolean(Config.Extra.answer, true)
            enterPager(PostSendActivity::class.java, bundle)
        }
    }

    override fun handleQuestionList(video: BaseBean<QuestionList?>?, loadType: Int) {
        refreshLayout.finishRefresh(Config.delayRefresh)
        questionsListAdapter.loadMoreModule.isEnableLoadMore = true
        mOnRefreshingListener?.let {
            if (LoadType.TYPE_REFRESH_SUCCESS == loadType || LoadType.TYPE_REFRESH_ERROR == loadType) {
                it.onDataRefreshingEnd()
            }
        }
        if (video == null) {
            if (questionsListAdapter.data.isNotEmpty()) {
                if (LoadType.TYPE_REFRESH_ERROR == loadType) {
                    ToastUtils.showShort(R.string.empty_network_error)
                } else {
                    questionsListAdapter.loadMoreModule.loadMoreFail()
                }
            } else {
                if (LoadType.TYPE_REFRESH_ERROR == loadType) {
                    questionsListAdapter.setEmptyView(errorView)
                } else {
                    questionsListAdapter.setEmptyView(getEmptyView())
                }
            }
        } else {
            if (video.isSuccess()) {
                video.data?.let {
                    if (LoadType.TYPE_REFRESH_SUCCESS == loadType) {
                        questionsListAdapter.setList(it.cmtList)
                    } else if (LoadType.TYPE_LOAD_MORE_SUCCESS == loadType) {
                        questionsListAdapter.addData(it.cmtList)
                    }
                    if (it.cmtList.isNullOrEmpty()) {
                        questionsListAdapter.setEmptyView(getEmptyView())
                        questionsListAdapter.loadMoreModule.loadMoreEnd()
                    } else {
                        questionsListAdapter.loadMoreModule.loadMoreComplete()
                    }
                }
            } else {
                ToastUtils.showShort(video.msg)
            }
        }

    }

    /* 供外部调用刷新数据 */
    fun flushData() {
        rvList.smoothScrollToPosition(0)
        refreshLayout.autoRefresh()
    }


    fun setOnRefreshingListener(onRefreshingListener: MainActivity.OnRefreshingListener) {
        this.mOnRefreshingListener = onRefreshingListener
    }


}